package com.congwulin.wechat.util;

import com.congwulin.wechat.entity.user.User;
import com.congwulin.wechat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
public class UserInfoUtil {

    @Autowired
    private UserService userService;

    public User getContent(){
        //获取到当前线程绑定的请求对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //已经拿到session,就可以拿到session中保存的用户信息了。
        String loginUserId = (String)request.getSession().getAttribute("loginUserId");
        return userService.getUserByID(loginUserId);
    }
}
